package com.hgh.springcloud.controller;

import com.hgh.springcloud.entities.CommonResult;
import com.hgh.springcloud.entities.Payment;
import com.hgh.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: hgh
 * @Description: restful风格：
 * 1、查询: post
 * 2、新增: get
 * 3、修改
 * 4、删除
 * @Date: Create in 11:27 2021/1/23
 */

@Slf4j
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String servePort;

    @PostMapping("/create")
    public CommonResult<Integer> create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("插入条数 {}", result);
        if (result > 0) {
            return new CommonResult<>(200, "成功", result);
        }
        return new CommonResult<>(444, "失败");
    }

    @GetMapping("/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("查询结果为：{}", paymentById);
        if (paymentById != null) {
            return new CommonResult<>(200, "成功,端口为：" + servePort, paymentById);
        }
        return new CommonResult<>(444, "失败");
    }
}
